package form;

import widget.QueryTable;
import widget.TPassword;
import widget.TTextField;

import javax.swing.*;
import java.util.HashMap;

/**
 * Created by LLAP on 2017/12/28.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Login extends Form {
    public Login() {
        super("登录", 500, 190);
        QueryTable table = new QueryTable();
        table.addRow().add(new TTextField("username", "用户名", true));
        table.addRow().add(new TPassword("password", "密码", true));
        this.add("Center", table.pack());
        JButton btnLogin = new JButton("登录");
        this.add("South", btnLogin);

        btnLogin.addActionListener(e -> {
            try {
                HashMap<String, Object> data = table.getData();
//                System.out.println(data);
                boolean isValid = entity.Security.validateUser(
                        data.get("username").toString(),
                        data.get("password").toString()
                );
                if (!isValid) throw new SecurityException("用户名或密码错误");
                else {
                    new Main().setVisible(true);
                    dispose();
                }
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        });
    }
    public static void main(String[] args) {
        new Login().setVisible(true);
    }
}
